﻿using AutoMapper;
using Comm100.Application.Services;
using Comm100.Framework.Auditing;
using Comm100.Framework.Authorization;
using Comm100.Public.Audit;
using Bot.Application.Dto;
using Bot.Application.Services;
using Bot.Domain.Bo;
using Bot.Domain.Entities;
using Bot.Domain.Interfaces;
using System;
using System.Collections.Generic;
using System.Text;
using Bot.Domain;

namespace Bot.Application.IntentAnswerInChannels.Service
{
    public class IntentAnswerInChannelAppService : BaseAppService, IIntentAnswerInChannelAppService
    {
        private IIntentAnswerInChannelDomainService _domainService;

        public IntentAnswerInChannelAppService(IIntentAnswerInChannelDomainService domainService) : base()
        {
            this._domainService = domainService;
        }

        [Authorization(BotPermission.MANAGE_BOTS)]
        //[Audit(BotEntity.INTENTANSWERINCHANNEL, AuditAction.DESTROY)]
        public void Delete(Guid id)
        {
            _domainService.Delete(id);
        }

        [Authorization(BotPermission.MANAGE_BOTS)]
        //[Audit(BotEntity.INTENTANSWERINCHANNEL, AuditAction.CREATE)]
        public IntentAnswerInChannelDto Add(Guid intentId, IntentAnswerInChannelCreateDto dto)
        {
            var intentAnswerInChannel = Mapper.Map<IntentAnswerInChannel>(dto);
            intentAnswerInChannel.IntentId = intentId;
            intentAnswerInChannel = _domainService.Create(intentAnswerInChannel);

            return Mapper.Map<IntentAnswerInChannelDto>(intentAnswerInChannel);
        }

        [Authorization(BotPermission.MANAGE_BOTS)]
        //[Audit(BotEntity.INTENTANSWERINCHANNEL, AuditAction.UPDATE)]
        public IntentAnswerInChannelDto Update(Guid id, IntentAnswerInChannelUpdateDto dto)
        {
            var intentAnswerInChannelUpdateBo = Mapper.Map<IntentAnswerInChannelUpdateBo>(dto);
            intentAnswerInChannelUpdateBo.Id = id;
            _domainService.Update(intentAnswerInChannelUpdateBo);
            var intentAnswerInChannel = Get(id, "answerInChannels,responses,form,fields,prompts");
            return intentAnswerInChannel;
        }

        public IntentAnswerInChannelDto Get(Guid id, string include="")
        {
            IntentAnswerInChannel intentAnswerInChannel = _domainService.Get(id,include);
            return Mapper.Map<IntentAnswerInChannelDto>(intentAnswerInChannel);
        }

        public IEnumerable<IntentAnswerInChannelDto> GetList(Guid intentId, string include="")
        {
            IEnumerable<IntentAnswerInChannel> list = _domainService.GetByIntentId(intentId,include);
            return Mapper.Map<IEnumerable<IntentAnswerInChannelDto>>(list);
        }
    }
}
